package mil.af.amc.adpetracker.service;

import mil.af.amc.adpetracker.dao.EquipmentTypeDao;
import mil.af.amc.adpetracker.dao.ItemModelDao;
import mil.af.amc.adpetracker.dao.ManufacturerDao;
import mil.af.amc.adpetracker.model.EquipmentType;
import mil.af.amc.adpetracker.model.ItemModel;
import mil.af.amc.adpetracker.model.Manufacturer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Please Fill in this Javadoc
 *
 * @author TSgt Jason Ferguson
 */
@Service("itemModelService")
public class ItemModelServiceImpl implements ItemModelService {

    private EquipmentTypeDao equipmentTypeDao;

    @Autowired
    public void setEquipmentTypeDao(EquipmentTypeDao equipmentTypeDao) {
        this.equipmentTypeDao = equipmentTypeDao;
    }

    private ItemModelDao itemModelDao;

    @Autowired
    public void setItemModelDao(ItemModelDao itemModelDao) {
        this.itemModelDao = itemModelDao;
    }

    private ManufacturerDao manufacturerDao;

    @Autowired
    public void setManufacturerDao(ManufacturerDao manufacturerDao) {
        this.manufacturerDao = manufacturerDao;
    }

    public void createModel(Map itemMap) {

        ItemModel model = new ItemModel();

        int manufacturerId = (Integer) itemMap.get("manufacturerId");
        Manufacturer manu = manufacturerDao.getById(manufacturerId);
        model.setManufacturer(manu);

        int equipmentTypeId = (Integer) itemMap.get("equipmentTypeId");
        EquipmentType eType = equipmentTypeDao.getById(equipmentTypeId);
        model.setType(eType);

        model.setName((String) itemMap.get("modelName"));

        model.setProcessorSpeed((Integer) itemMap.get("processorSpeed"));

        model.setScreenSize((Integer) itemMap.get("screenSize"));

        itemModelDao.saveOrUpdate(model);


    }

    public void editModel(Map itemMap) {

        int itemModelId = (Integer) itemMap.get("modelId");
        ItemModel model = itemModelDao.getById(itemModelId);

        int manufacturerId = (Integer) itemMap.get("manufacturerId");
        Manufacturer manu = manufacturerDao.getById(manufacturerId);
        model.setManufacturer(manu);

        int equipmentTypeId = (Integer) itemMap.get("equipmentTypeId");
        EquipmentType eType = equipmentTypeDao.getById(equipmentTypeId);
        model.setType(eType);

        model.setName((String) itemMap.get("modelName"));

        model.setProcessorSpeed((Integer) itemMap.get("processorSpeed"));

        model.setScreenSize((Integer) itemMap.get("screenSize"));

        itemModelDao.saveOrUpdate(model);

    }

    public void deleteModel(Map itemMap) {

        int itemModelId = (Integer) itemMap.get("modelId");
        ItemModel model = itemModelDao.getById(itemModelId);

        itemModelDao.delete(model);
    }

    public ItemModel getById(int id) {

        return itemModelDao.getById(id);
    }

    public List<ItemModel> getAll() {

        return itemModelDao.getAll();
    }

    public List<ItemModel> getByManufacturer(int manufacturerId) {

        Manufacturer manu = manufacturerDao.getById(manufacturerId);
        return getByManufacturer(manu);

    }

    public List<ItemModel> getByManufacturer(Manufacturer manu) {

        return itemModelDao.getAllByManufacturer(manu);
    }

    public List<ItemModel> getByEquipmentType(int equipmentTypeId) {

        EquipmentType type = equipmentTypeDao.getById(equipmentTypeId);
        return getByEquipmentType(type);
    }

    public List<ItemModel> getByEquipmentType(EquipmentType equipmentType) {

        return itemModelDao.getItemModelsByEquipmentType(equipmentType);

    }

    public List<ItemModel> getByEquipmentTypeAndManufacturer(int equipmentTypeId, int manufacturerId) {

        EquipmentType type = equipmentTypeDao.getById(equipmentTypeId);
        Manufacturer manu = manufacturerDao.getById(manufacturerId);

        return itemModelDao.getAllByItemTypeAndManufacturer(type, manu);
    }
}
